import { getCompanyID, getPosIDs, getTenantID, getUserDepID, getUserID, requestFunCompon } from '@/utils';
import { useMemo } from 'react';
import { useRequest } from 'umi';

const roleManageTable = () => {
  const getTableDataSource = useRequest(
    () => {
      // let reqUrl = `/roleController/treeGrid/${getPosIDs()}`;
      let reqUrl = `/roleController/treeGrid/${getUserID()}`;
      return requestFunCompon('GET', reqUrl);
    },
    {
      manual: false,
      onSuccess(data: any, params: any): void {},
    },
  );

  /* 列表数据 */
  let dataSource = useMemo(() => {
    if (getTableDataSource?.data && getTableDataSource?.data?.msgFlag) {
      return getTableDataSource?.data?.obj;
    } else {
      return [];
    }
  }, [getTableDataSource?.data]);

  return {
    reloadRoleTable: getTableDataSource?.run,
    tableDataSource: dataSource,
    tableLoading: getTableDataSource.loading,
  };
};

// 新增接口 方法
const addEditRoleMange = (bodyPayload: any, reqType: string) => {
  let reqUrl = reqType == 'add' ? '/roleController/add' : reqType == 'edit' ? '/roleController/edit' : '';
  let methodType = reqType == 'add' ? 'POST' : reqType == 'edit' ? 'PUT' : '';
  if (reqUrl && methodType) {
    return requestFunCompon(methodType, reqUrl, bodyPayload);
  }
};

// 获取详情
const getRoleByID = (bodyParams: any) => {
  const RoleDetailInfo = useRequest(
    () => {
      let reqUrl = `/roleController/getRoleByID/${bodyParams?.id}`;
      return requestFunCompon('GET', reqUrl);
    },
    {
      manual: bodyParams && bodyParams?.id ? false : true, //页面初始请求为true
      onSuccess: (res) => {
        if (res && res?.msgFlag) {
          // message.success(res?.msg)
        }
      },
    },
  );

  // 处理数据
  let detailInfoData = useMemo(() => {
    return RoleDetailInfo?.data?.obj;
  }, [RoleDetailInfo.data]);

  return {
    detailInfoData: detailInfoData,
    infoLoading: RoleDetailInfo.loading,
    infoRefresh: RoleDetailInfo.run,
  };
};

// 开启关闭 资源状态
const switchRoleIsOpen = (bodyPayload: any) => {
  let reqUrl = `/roleController/modifyState`;
  let methodType = `PUT`;
  if (reqUrl && methodType) {
    return requestFunCompon(methodType, reqUrl, {
      modifyerID: getUserID(),
      modifyPositionID: getPosIDs(),
      modifyDepartmentID: getUserDepID(),
      modifyCompanyID: getCompanyID(),
      ...bodyPayload,
    });
  }
};

// 获取岗位下资源
const getResource = () => {
  const rseourceData = useRequest(
    () => {
      let bodyParams = {
        // positionID: getPosIDs(),
        userID: getUserID(),
        dicTypeIds: `${getTenantID()}0000`,
        pID: null,
        resourceType: null,
      };
      // let reqUrl = `/roleController/getResource/${getPosIDs()}`;
      let reqUrl = `/roleController/getResource`;
      return requestFunCompon('POST', reqUrl, bodyParams);
    },
    {
      manual: false,
      onSuccess(data: any, params: any): void {},
    },
  );
  /* 列表数据 */
  let resourDataList = useMemo(() => {
    if (rseourceData?.data && rseourceData?.data?.msgFlag) {
      return rseourceData?.data?.obj;
    } else {
      return [];
    }
  }, [rseourceData?.data]);

  return {
    roleReload: rseourceData?.run,
    roleDataList: resourDataList,
    roleDataLoading: rseourceData.loading,
  };
};

export { roleManageTable, switchRoleIsOpen, getRoleByID, addEditRoleMange, getResource };
